<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\tradi\controller;

use app\index\model\Article;
use think\Db;
use think\Request;

class About extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->currentModel = new Article();//实例化当前模型
    }

    /**
     * 管理團隊
     * @return mixed
     */
    public function aboutT()
    {
        $this->renderData(100);//渲染数据，分类cat_id：管理團隊
        return $this->fetch();
    }

    /**
     * 首彩榮譽
     * @return mixed
     */
    public function aboutR()
    {
        $this->renderData(93);//渲染数据，分类cat_id：首彩榮譽
        return $this->fetch();
    }

    /**
     * 首彩榮譽详情页：国际
     * @return mixed
     */
    public function aboutRDetail()
    {
        $this->renderData(95);//渲染数据，分类cat_id：首彩榮譽

        $data_list = $this->currentModel->getDataList(['cat_id' => 95]);//获取分类下的数据列表
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }

    /**
     * 首彩榮譽详情页：国内
     * @return mixed
     */
    public function aboutRcDetail()
    {
        $this->renderData(94);//渲染数据，分类cat_id：首彩榮譽

        $data_list = $this->currentModel->getDataList(['cat_id' => 94]);//获取分类下的数据列表
        $this->assign('data_list', $data_list);
        return $this->fetch();
    }

    /**
     * 合作商家
     * @return mixed
     */
    public function aboutS()
    {
        $_GET['limit'] = 100;//每页数量
        $this->renderData(96);//渲染数据，分类cat_id：合作商家
        return $this->fetch();
    }

    /**
     * 招賢納士
     * @return mixed
     */
    public function aboutH()
    {
        $this->renderData(97);//渲染数据，分类cat_id：招賢納士
        return $this->fetch();
    }

    /**
     * 聯系我們
     * @return mixed
     */
    public function aboutC()
    {
        $this->renderData(98);//渲染数据，分类cat_id：聯系我們
        return $this->fetch();
    }

    /**
     * 首彩版图
     * @return mixed
     */
    public function aboutDomain()
    {
        $this->renderData(150);//渲染数据，分类cat_id：首彩版图
        return $this->fetch();
    }

    /**
     * 渲染数据，公共方法
     * @param $cat_id int 按分类id搜索
     */
    public function renderData($cat_id = 0)
    {
        $cat_list = $this->currentModel->getCatList(['cat_type'=>208, 'pid'=>0]);//获取版块(品牌中心)下的分类列表
        foreach ($cat_list as $k=>$v) {
            $cat_list[$k]['is_display'] = $v['cat_id'] == $cat_id ? 1 : 0;//标记当前分类，前端显示
            switch ($v['cat_id']) {
                case 150 :
                    $cat_list[$k]['link'] = url('aboutDomain');//首彩版图
                    break;
                case 100 :
                    $cat_list[$k]['link'] = url('aboutT');//管理團隊
                    break;
                case 93 :
                    $cat_list[$k]['link'] = url('aboutR');//首彩榮譽
                    break;
                case 96 :
                    $cat_list[$k]['link'] = url('aboutS');//合作商家
                    break;
                case 97 :
                    $cat_list[$k]['link'] = url('aboutH');//招賢納士
                    break;
                case 98 :
                    $cat_list[$k]['link'] = url('aboutC');//聯系我們
                    break;
                default:
                    $cat_list[$k]['link'] = '';
                    break;
            }
        }
        $this->assign('cat_list', $cat_list);

        $data_list = $this->currentModel->getDataList(['cat_id' => $cat_id]);//获取分类下的数据列表
        $this->assign('data_list', $data_list);

        //获取banner
        $banner = $this->currentModel->getBanner('banner', 'AG');
        $this->assign('banner', $banner);

        $this->assign('web', ['title' => '關于我們']);//网站标题
    }

    /**
     * 接口：获取版图地址列表
     * @return mixed
     */
    public function getDomainList()
    {
        $list = Db::name('basic_address')->where('type', 2)->field('contact as name,province,city,address,tel')->order('sort_num asc')->select();
        $data = [];
        $i = 0;
        foreach ($list as $k=>$v) {
            //序号
            if (!isset($data['city_list'][$v['province']])) $i++;

            //省份列表
            $data['province_list'][$v['province']]['name'] = Db::name('basic_region')->where('id', $v['province'])->value('area_name');
            $data['province_list'][$v['province']]['value'] = $i;

            //城市列表
            $data['city_list'][$v['province']]['n'] = $i;
            $data['city_list'][$v['province']]['b'][$k]['city'] = Db::name('basic_region')->where('id', $v['city'])->value('area_name');
            $data['city_list'][$v['province']]['b'][$k]['address'] = $v['address'];
            $data['city_list'][$v['province']]['b'][$k]['tel'] = $v['tel'];
            $data['city_list'][$v['province']]['b'][$k]['name'] = $v['name'];

            //排序列表
            $data['sort_list'][$v['province']]['name'] = $i;
            $data['sort_list'][$v['province']]['value'] = $i;
        }

        //格式化，下标从0开始
        $data['province_list'] = array_values($data['province_list']);
        $data['city_list'] = array_values($data['city_list']);
        $data['sort_list'] = array_values($data['sort_list']);
        foreach ($data['city_list'] as $k=>$v) {
            $data['city_list'][$k]['b'] = array_values($v['b']);
        }
        return json(['code'=>1, 'msg'=>'获取成功', 'data'=>$data]);
    }

}

